/**
 * 数据库反转处理
 */

$.mapper = {
	
	buildDataBaseConnectionParam:function(){
		
		var driverName = $("#driverName").val();
		
		var url = $("#url").val();
		
		var userName = $("#userName").val();
		
		var passWord = $("#passWord").val();
		
		var schema = $("#schema").val();
		
		if(driverName == "" || url=="" || userName == "" || passWord == "" || schema == ""){
			
			alertMsg.warn(igdConstant.inputDoneConnectionInfo);

			return false;

		}else{
			
			return "?driverName="+driverName+"&url="+url+"&userName="+userName+"&passWord="+passWord+"&schema="+schema;

		}
	},
		
	getTables:function(){
		
		if(this.buildDataBaseConnectionParam()){
			
			var reqUrl = "/dev/ajax/getTables.action"+this.buildDataBaseConnectionParam();
			
			ajaxReq(reqUrl,function(data){

				$("#tables").empty();

				var dataSize = data.rspDataSize;

				$("#tables").append("<option value=''>请选择数据库表</option>");

				for(var i=0;i<dataSize;i++){

					var tableName = data.rspData[i];

					$("#tables").append("<option value=\""+tableName+"\">"+tableName+"</option>");

				}

				$("#tables").change(function(){
					
					var tableName = $(this).val();
					
					if(tableName != ""){
						
						$.mapper.getMapperCode(tableName);
						
					}
				});

			},false);
			
		}
	},
	
	getMapperCode:function(tableName){
		
		if(this.buildDataBaseConnectionParam()){
			
			var reqUrl = "/dev/ajax/getMapperCode.action"+this.buildDataBaseConnectionParam()+"&tableName="+tableName;
			
			ajaxReq(reqUrl,function(data){

				var javaCode = data.rspData[0];
					
				var ibatisCode = data.rspData[1];
					
				if(javaCode != ""){
						
					$("#javaCode").val(javaCode);
						
				}
				
				if(ibatisCode != ""){
						
					$("#ibatisCode").val(ibatisCode);
				}
					
			},true);
		}
	} 
};